package com.qms.repast.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.qms.repast.R;
import com.qms.repast.entity.resbean.CuisineMenuBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @author 泅渡者
 * @date 2018/9/19 0019
 * @describe:
 */
public class CuisineMenuAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

	private OnItemClickListener mItemClickListener;
	private List<CuisineMenuBean> dataList;
	private Context context;
	private int currentIndex = 0;

	public CuisineMenuAdapter (Context context) {
		this.context = context;
		dataList = new ArrayList<>();
	}

	@NonNull
	@Override
	public RecyclerView.ViewHolder onCreateViewHolder (@NonNull ViewGroup parent, int viewType) {
		LayoutInflater mInflater = LayoutInflater.from(context);
		RecyclerView.ViewHolder holder = null;
		View v = mInflater.inflate(R.layout.layout_cuisine_menu_item, parent, false);
		holder = new ViewHolder(v);

		return holder;
	}

	@Override
	public void onBindViewHolder (@NonNull RecyclerView.ViewHolder holder, int position) {
		((ViewHolder) holder).tvMenu.setText(dataList.get(position).getCategoryTitle());
		if (currentIndex == position) {
			((ViewHolder) holder).tvMenu.setSelected(true);
			((ViewHolder) holder).tvMenu.setTextColor(Color.parseColor("#FF5A00"));
		} else {
			((ViewHolder) holder).tvMenu.setSelected(false);
			((ViewHolder) holder).tvMenu.setTextColor(Color.parseColor("#9599A5"));
		}
		holder.itemView.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick (View v) {
				mItemClickListener.onItemClick(v);
			}
		});
	}

	@Override
	public int getItemCount () {
		return dataList == null ? 0 : dataList.size();
	}

	/**
	 * 设置当前选择
	 *
	 * @param currentIndex
	 */
	public void setCurrentIndex (int currentIndex) {
		this.currentIndex = currentIndex;
		notifyDataSetChanged();
	}

	/**
	 * 获取当前选中的数据
	 *
	 * @param position
	 */
	public CuisineMenuBean getCurrentItem (int position) {
		return dataList.get(position);
	}

	/**
	 * 添加数据集合
	 *
	 * @param cuisineMenuBeans
	 */

	public void addData (List<CuisineMenuBean> cuisineMenuBeans) {
		for (CuisineMenuBean cuisineMenuBean : cuisineMenuBeans) {
			this.dataList.add(cuisineMenuBean);
		}
		notifyDataSetChanged();
	}

	/**
	 * 点击事件
	 */
	public interface OnItemClickListener {
		/**
		 * 点击
		 *
		 * @param view
		 */
		void onItemClick (View view);
	}

	public void setmItemClickListener (OnItemClickListener mItemClickListener) {
		this.mItemClickListener = mItemClickListener;
	}

	class ViewHolder extends RecyclerView.ViewHolder {
		@BindView(R.id.tv_menu)
		TextView tvMenu;

		public ViewHolder (View itemView) {
			super(itemView);
			ButterKnife.bind(this, itemView);
		}
	}


}
